package com.ss.controller;

import com.ss.bean.TblUserRecord;
import com.ss.result.Permission;
import com.ss.result.Permissions;
import com.ss.result.R;
import com.ss.result.UserInfo;
import com.ss.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: ljy.s
 * @Date: 2022/9/16 - 09 - 16 - 16:36
 */
/*控制层
 * 控制层->业务层->持久层
 * */
// @RestController = @Controller + @RequestBody
@RestController
public class LoginController {

    @Autowired
    private LoginService loginService;

    /**
     * 登录测试
     *
     * @return
     */
    // @CrossOrigin(origins = "*", allowedHeaders = "*", methods = {}, allowCredentials = "true") // 跨域
    @RequestMapping("/test/login")
    public String test() {
        System.out.println("测试登录...");
        return "登录测试...";
    }

    /**
     * 解决前端报错（可处理可不处理）
     *
     * @return
     */
    @RequestMapping("/auth/2step-code")
    public boolean step_code() {
        //System.out.println("此请求是前端框架默认的请求，可以不做处理，也可以通过这种方式处理");
        System.out.println("2step-code");
        return true;
    }

    // 解决前端报错
    /*@RequestMapping("/auth/2step-code")
    public boolean step_code2(){
        System.out.println("此请求是前端框架带的默认请求，可以不做任何处理，也可以在前端将其删除");
        System.out.println("step_code2");
        return true;
    }*/

    /**
     * 登录接口
     *
     * @param username 账号
     * @param password 密码
     * @return
     */
    @RequestMapping("/auth/login")
    public R login(@RequestParam(value = "username") String username,
                   @RequestParam(value = "password") String password,
                   HttpSession session) {
        System.out.println("login...");
        /*System.out.println("username：" + username);
        System.out.println("password：" + password);*/

        TblUserRecord tblUserRecord = loginService.login(username, password);
        tblUserRecord.setToken(tblUserRecord.getUserName());
        // 将用户信息写入Session中
        session.setAttribute("userRecord", tblUserRecord);
        R r = new R(200, "ok", tblUserRecord);
        //System.out.println(tblUserRecord);
        return r;
    }

    /**
     * 前端要拿用户信息
     *
     * @return 返回给前端用户信息
     */
    @RequestMapping("/user/info")
    public R getInfo(HttpSession session) {
        //System.out.println(session.getAttribute("userRecord"));

        // 获取用户信息
        TblUserRecord tblUserRecord = (TblUserRecord) session.getAttribute("userRecord");
        // 获取用户对应的功能模块
        String[] rolePrivileges = tblUserRecord.getTblRole().getRolePrivileges().split("-");
        // 拼接需要返回的数据对象格式
        Permissions permissions = new Permissions();
        List<Permission> permissionList = new ArrayList<>();
        for (String s : rolePrivileges) {
            permissionList.add(new Permission(s));
        }
        permissions.setPermissions(permissionList);
        UserInfo userInfo = new UserInfo(tblUserRecord.getUserName(), permissions);
        return new R(userInfo);
    }

    /**
     * 处理登出业务
     *
     * @param session 清空session
     */
    @RequestMapping("/auth/logout")
    public String logOut(HttpSession session) {
        System.out.println("logout");
        // 将session设置为失效
        session.invalidate();
        return "";
    }
}
